package cn.hznc.controller.service.inventory;

import cn.hznc.mapper.ToolInventoryLocationHistoryMapper;
import cn.hznc.mapper.ToolInventoryLocationMapper;
import cn.hznc.repository.ToolInventoryLocationHistoryRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author：Administrator
 * @Date：2023/5/16 15:49
 */
@Service
public class ToolInventoryLocationHistoryService{

    @Resource
    private ToolInventoryLocationHistoryRepository toolInventoryLocationHistoryRepository;

    public List<String> buildDateList() {
        //构建进7天的日期列表
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        cal.setTime(date);
        List<String> dateList = new ArrayList<String>();
        for(int i=0;i< 7;i++){
            dateList.add(sdf.format(cal.getTime()));
            cal.add(Calendar.DATE,-1);
        }
        return dateList;
    }

    public List<Map<String , Object>> getInventorySnapshot(Integer toolId , Integer assemblyToolId){
        List<Map<String , Object>> retList = new ArrayList<>();
        //获取近7天的库存快照数据
        List<String> dateList = buildDateList();
        for (int i = dateList.size() - 2; i >= 0; i--) {
            Map<String , Object> retMap = new HashMap<>();
            List<Map<String, Integer>> inventoryStatus = toolInventoryLocationHistoryRepository.getInventoryStatus(dateList.get(i+1) , dateList.get(i) , toolId , assemblyToolId);
            retMap.put("date",dateList.get(i+1));
            retMap.put("inventoryStatus",inventoryStatus);
            retList.add(retMap);
        }
        return retList;
    }

}
